﻿///<reference path="Scripts/typings/gruntjs/gruntjs.d.ts" />

"use strict";

import moduleUtils = require('../Sustainalytics.Core.Node/modules/ModuleUtils/ModuleUtils');

import fs = require('fs');
import path = require('path');

declare var __dirname: string;

export = gruntFunc;
var gruntFunc = (grunt: IGrunt) => {
    var moduleHost = new moduleUtils.ModuleHost(
        __dirname + '/../Sustainalytics.ClientInterface.Service/fs-config.json',
        __dirname + '/../Sustainalytics.ClientInterface.UI/modules/ShellModule/module.paths.json',
        false);

    // all module.angular to be bundled
    var bundleModuleAngularRegex = moduleHost.createRegexModuleAngular(
        [
            /_Libs/i
        ]);

    // only part of modules used for bundling
    var bundleModuleJsonRegex = moduleHost.createRegexModuleJson(
        [
            /_Libs/i
        ]);

    // all modules
    var moduleJsonRegex = moduleHost.createRegexModuleJson();

    var copyConfig = moduleHost.createCopyConfig(__dirname + '/../Sustainalytics.ClientInterface.Service/bin/Release/website');

    var moduleDirectory = new moduleUtils.ModuleDirectory(
        __dirname + '/../',
        __dirname + '/../Sustainalytics.ClientInterface.UI/modules/ShellModule/module.paths.json',
        new RegExp(moduleJsonRegex, "i"));

    //console.log('\n moduleAngularRegex:');
    //console.log(moduleAngularRegex);
    //console.log('\n moduleJsonRegex:');
    //console.log(moduleJsonRegex);
    //console.log('\n copyConfig:');
    //console.log(copyConfig);
    //console.log('\n');

    // create bundling utility
    var bundling = new moduleUtils.ModuleBundling(moduleDirectory,
        {
            bundles:
            [
                // all module.angular combined from all modules
                {
                    files:
                    [
                        { pattern: bundleModuleAngularRegex }
                    ],
                    out: 'Sustainalytics.ClientInterface.UI\\modules\\bundles\\module.angular.min.js',
                    dependencies: ['modules/_Libs/Angular/module.json'],
                    ignoreModuleDependencies: true
                },

                // all modules except some of the _Libs
                {
                    files:
                    [
                        { pattern: bundleModuleJsonRegex, moduleFilesPattern: 'module.angular.js$', moduleFilesPatternReverse: true }
                    ],
                    out: 'Sustainalytics.ClientInterface.UI\\modules\\bundles\\{{0}}.min.js',
                    dependencies: ['modules/bundles/module.angular.min']
                },

                // AmCharts
                {
                    files:
                    [
                        { pattern: '_Libs\\\\(AmCharts)\\\\module.json$', moduleFilesPattern: 'module.angular.js$', moduleFilesPatternReverse: true }
                    ],
                    out: 'Sustainalytics.ClientInterface.UI\\modules\\bundles\\{{0}}.min.js',
                    dependencies: []
                },

                // Angular
                {
                    files:
                    [
                        { pattern: '_Libs\\\\(Angular)\\\\module.json$', moduleFilesPattern: 'module.angular.js$', moduleFilesPatternReverse: true }
                    ],
                    out: 'Sustainalytics.ClientInterface.UI\\modules\\bundles\\{{0}}.min.js',
                    dependencies: []
                },

                // Angular-1.2
                {
                    files:
                    [
                        { pattern: '_Libs\\\\(Angular-1\\.2)\\\\module.json$', moduleFilesPattern: 'module.angular.js$', moduleFilesPatternReverse: true }
                    ],
                    out: 'Sustainalytics.ClientInterface.UI\\modules\\bundles\\{{0}}.min.js',
                    dependencies: [],
                    browser: [{ name: 'MSIE', vers: "8", comp: "lte" }]
                },

                // Angular-last
                {
                    files:
                    [
                        { pattern: '_Libs\\\\(Angular-last)\\\\module.json$', moduleFilesPattern: 'module.angular.js$', moduleFilesPatternReverse: true }
                    ],
                    out: 'Sustainalytics.ClientInterface.UI\\modules\\bundles\\{{0}}.min.js',
                    dependencies: [],
                    browser: [{ name: 'MSIE', vers: "8", comp: "lte", inverse: true }]
                },

                // jQuery
                {
                    files:
                    [
                        { pattern: '_Libs\\\\(jQuery)\\\\module.json$', moduleFilesPattern: 'module.angular.js$', moduleFilesPatternReverse: true }
                    ],
                    out: 'Sustainalytics.ClientInterface.UI\\modules\\bundles\\{{0}}.min.js',
                    dependencies: []
                },

                // Libs
                {
                    files:
                    [
                        { pattern: '_Libs\\\\(Libs)\\\\module.json$', moduleFilesPattern: 'module.angular.js$', moduleFilesPatternReverse: true }
                    ],
                    out: 'Sustainalytics.ClientInterface.UI\\modules\\bundles\\{{0}}.min.js',
                    dependencies: []
                },

                // Shims module
                {
                    files:
                    [
                        { pattern: '_Libs\\\\(Shims)\\\\module.json$', moduleFilesPattern: 'module.angular.js$', moduleFilesPatternReverse: true }
                    ],
                    out: 'Sustainalytics.ClientInterface.UI\\modules\\bundles\\{{0}}.min.js',
                    dependencies: [],
                    browser: [{ name: 'MSIE', vers: "8", comp: "lte" }]
                }
            ]
        });

    //bundling.moduleDirectory.createAllModuleReferences();

    //// write module definitions JSON
    bundling.moduleDirectory.writeModuleDefinitionsJson(__dirname + '\\..\\Sustainalytics.ClientInterface.UI\\modules\\bundles\\modules.json');

    //// write bundling definitions JSON
    bundling.writeBundleMapsJson(__dirname + '\\..\\Sustainalytics.ClientInterface.UI\\modules\\bundles\\bundles.json', false);

    // create Uglify config
    var uglifyConfig =
        {
            options:
            {
                mangle: true,
                sourceMap: true,
                banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' + '<%= grunt.template.today("yyyy-mm-dd HH:mm:ss") %> */\n'
            },
            build:
            {
                files: bundling.createGruntUglifyFilesConfig(__dirname)
            }
        };

    grunt.initConfig(
        {
            pkg: grunt.file.readJSON('package.json'),

            uglify: uglifyConfig,

            copy:
            {
                main:
                {
                    files: copyConfig
                }
            }
        });

    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-copy');

    grunt.registerTask('default', ['uglify', 'copy']);
    //grunt.registerTask('default', []);
}